home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib43 / mntlib / ffs.c < prev    next >
C/C++ Source or Header  |  1993-09-15  |  755b  |  42 lines

  1. /*
  2.  *  ffs.c - find the lowest bit set
  3.  *  Returns a 2-exponent of position + 1 and 0 for 0
  4.  *  Michal Jaegermann, <ntomczak@vm.ucs.ualberta.ca>
  5.  *  10 July 1993
  6.  *  This piece of code is in a Public Domain
  7.  */
  8.  
  9. #include <support.h>
  10.  
  11. int
  12. ffs(bits)
  13.   int bits;
  14. {
  15.     register int count;
  16.  
  17.     if (0 == bits)
  18.     return 0;
  19.  
  20.     bits &= -bits;
  21. #ifndef __MSHORT__
  22.     count = (bits & 0x0000ffff ? 16 : 32);
  23.     if (bits & 0x00ff00ff)
  24.     count -= 8;
  25.     if (bits & 0x0f0f0f0f)
  26.     count -= 4;
  27.     if (bits & 0x33333333)
  28.     count -= 2;
  29.     if (bits & 0x55555555)
  30.     count -= 1;
  31. #else
  32.     count = (bits & 0x00ff ? 8 : 16);
  33.     if (bits & 0x0f0f)
  34.     count -= 4;
  35.     if (bits & 0x3333)
  36.     count -= 2;
  37.     if (bits & 0x5555)
  38.     count -= 1;
  39. #endif /* __MSHORT__ */
  40.     return count;
  41. }
  42.